package samples.java.algorithm.sort;

/**
 * @author wang.yuchuan
 * @createdAt 2018/12/9
 */
public class SelectionSort {

    public static void sort(int[] arr) {
        int length = arr.length;
        int minIndex, temp;

        for (int i = 0; i < length - 1; i++) {
            minIndex = i;
            for (int j = i + 1; j < length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }

            temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {3, 44, 5, 67, 4, 23, 42, 56, 78, 91, 2, 7, 9, 12, 16};

        sort(arr);

        for (int i : arr) {
            System.out.print(i + " ");
        }
    }
}
